﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net.Sockets;
using System.IO;
using System.Numerics;
using eVoteClient.util;

namespace eVoteClient
{
    public partial class ViewOpenPoll : Form
    {
        public int pollID;
        string poll_reply;
        public ViewOpenPoll( int pID, string rep)
        {
            this.pollID = pID;
            poll_reply = rep;
            InitializeComponent();
        }
        public ViewOpenPoll()
        {InitializeComponent();}

        private void Form1_Load(object sender, EventArgs e)
        {
            this.Location = new Point(350, 200);
            this.Size = new Size(572, 324);

            votingPanel.Visible = false;
            myVoteLabel.Visible = false;
            var socket = global.socket;

            // Getting my vote
            socket.writer.WriteLine("CHK_VOT#"+pollID+"\n");
            socket.writer.Flush();
            string reply = socket.reader.ReadLine();

            string[] idArray = reply.Split('#');
            if (idArray[1] == "0")
            {
                votingPanel.Visible = true;
            }
            else
            {
                myVoteLabel.Text = "Your vote key is "+idArray[1]+"\nYou can compare it with the one sent to your mail";
                myVoteLabel.Visible = true;
            }

            // Getting voters names
            //MessageBox.Show(pollID.ToString());
            socket.writer.WriteLine("TRC_REQ#" + pollID + "\n");
            socket.writer.Flush();
            reply = socket.reader.ReadLine();
            //MessageBox.Show(reply);
            List<string> voters = new List<string>();
            if (reply.Contains("#"))
            {
                string[] users = reply.Split('#');
                for (int i = 1; i < users.Length; i++)
                {
                    voters.Add(users[i]);
                }
            }
            else
                voters.Add("No Voters Yet!");

            votersList.DataSource = voters;


            //IF ADMIN THEN HIDE SUBMIT BUTTON
            string[] values = poll_reply.Split('#');

            string code = values[0];
            string endDate = values[1];
            BigInteger b = BigInteger.Parse(values[2]);
            BigInteger g = BigInteger.Parse(values[3]);
            BigInteger n = BigInteger.Parse(values[4]);
            
            string question = values[5];

            questionLbl.Text = question;
            for (int i = 6; i < values.Length; i++)
            {
                answersCmb.Items.Add(values[i]);
            }
            answersCmb.SelectedIndex = 0;
            


        }


        private void s_Click(object sender, EventArgs e)
        {
            string[] values = poll_reply.Split('#');
            BigInteger b = BigInteger.Parse(values[2]);
            BigInteger g = BigInteger.Parse(values[3]);
            BigInteger n = BigInteger.Parse(values[4]);

            BigInteger actualMessage = BigInteger.Pow(b, answersCmb.SelectedIndex);
            //MessageBox.Show(actualMessage.ToString()+" b g n   "+b.ToString()+"    "+g.ToString()+"    "+n.ToString());
            

            var socket = global.socket;
            var encVote = util.Encryption.paillierEncrypt(b, g, n, answersCmb.SelectedIndex);
            //MessageBox.Show(encVote.ToString());
            socket.writer.WriteLine("MAK_VOT#" + pollID + "#" + encVote);
            socket.writer.Flush();
            string reply_code = socket.reader.ReadLine().Split('#')[1];
            if (reply_code.Equals("0"))
                MessageBox.Show("Sorry, you already voted in this poll before");
            else
            {
                MessageBox.Show("Your vote has been submitted. Thank you.");
                var mail = "Your vote has been submitted successfully.\n";
                mail+="This email is sent from the application you used to submit your vote.\n";
                mail+="Below is your vote reference number, you can use this number when tracking your vote later :\n";
                mail+= ""+encVote;
                //Encryption.sendMail(global.Email, "Your vote reference number", mail);
            }
            
        }

        private void linkLabel1_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            new Home().Show();
            this.Hide();
        }
    }
}